// 点击登录按钮
var btn = document.querySelector('.submit input');
var userName = document.querySelector('.userName input');
var userPwd = document.querySelector('.userpwd  input');
var body = document.querySelector('body');
btn.onclick = function() {
   isLogin(userName.value, userPwd.value);
};

// 注册键盘事件按下回车键登录
body.onkeyup = function(e) {
    if(e.key == 'Enter') {
      isLogin(userName.value, userPwd.value);
    }
}


// 判断用户是否登录成功
function isLogin(uname, pwd) {
    // 获取本地存储中的数据
    var res = JSON.parse(localStorage.getItem("userInfo")) || [];
    if(uname == ""  || pwd == "") {
        userName.style.border = "1px solid red";
        userPwd.style.border = "1px solid red";
        userName.className = 'dou';
        userPwd.className = 'dou';
        return;
    }
    var flg = res.filter(function(item) {
       return  (item.uname == uname || item.phone == uname) && item.password == pwd;
    });
    if(flg.length > 0 ) {
       location.href= "./index.html";
       // 将当前登录用户信息保存到本地存储中
       localStorage.setItem("longinName",uname);
    }else {
      userName.style.border = "1px solid red";
      userPwd.style.border = "1px solid red";
      userName.className = 'dou';
      userPwd.className = 'dou';
      userName.value = '';
      userPwd.value = '';
    }
}

userName.onfocus = function() {
   this.className = '';
};

userPwd.onfocus = function() {
   this.className = '';
};
 

